#include <QDebug>
#include <QSqlQuery>
#include <QSqlError>

#include "gui/customerview.h"
#include "gui/shinylistitem.h"
#include "gui/customerviewadddialog.h"
#include "gui/customerviewdetailsdialog.h"
#include "gui/customervieweditdialog.h"
#include "core/customermodel.h"
#include "core/customercontroller.h"

void CustomerView::initViewComponents()
{
    // No "super" in C++ - this calls the superclass implementation
    BaseView::initViewComponents();
    qDebug() << "CustomerView::initUiComponents() called";
    this->topLabel->setText("Double-click a Customer to see more details.");
    this->addButton->setText("Add Customer");
    this->hideReportsWidgets();
}

void CustomerView::initViewSignalsAndSlots()
{
    BaseView::initViewSignalsAndSlots();

    connect(this->addButton, SIGNAL(clicked()), this, SLOT(onAddButtonClicked()));
    connect(this, SIGNAL(detailsRequested(BaseModel*)), this, SLOT(onDetailsRequested(BaseModel*)));
}

void CustomerView::onAddButtonClicked()
{
    CustomerModel *customer = NULL;
    CustomerController *ctrl = (CustomerController *) this->getController();
    CustomerViewAddDialog *dialog = new CustomerViewAddDialog(this);

    dialog->exec(); // execute the dialog
    customer = dialog->getCustomer(); // gets a customermodel from the input

    // If "Cancel" is pressed. getCustomer() returns null
    if (customer) {
    ctrl->addCustomer(customer);
    ctrl->getAll();
    }

    delete dialog;
}

void CustomerView::onDetailsRequested(BaseModel *model)
{
    int result;
    CustomerViewDetailsDialog *dialog = new CustomerViewDetailsDialog(this);
    CustomerController *ctrl = (CustomerController *) this->getController();
    CustomerModel *customer = (CustomerModel *) model;

    dialog->setCustomer(customer);
    result = dialog->exec();

    if (result == QDialog::Accepted) {
        CustomerViewEditDialog *editDialog = new CustomerViewEditDialog(this, customer);
        editDialog->exec();
        customer = editDialog->getCustomer();

        if (customer) {
            ctrl->updateCustomer(customer);
            ctrl->getAll();
        }

        delete editDialog;
    }

    delete dialog;
}

CustomerView::CustomerView(QWidget *parent) :
    BaseView(parent)
{
    this->initViewComponents();
    this->initViewSignalsAndSlots();
}

void CustomerView::onDataUpdate(QList<BaseModel *> models)
{
    CustomerModel *customer = NULL;
    ShinyListItem *listItem = NULL;
    int i;

    qDebug() << "CustomerView received update";
    BaseView::onDataUpdate(models);

    this->slist->clear();
    for (i = 0; i < models.length(); i++) {
        listItem = new ShinyListItem;
        listItem->setItemIcon("res/customer.png");
        customer = (CustomerModel *) models.at(i);

        qDebug() << "CustomerView: Got customer w/ ID " << customer->getID();

        listItem->setMainText(customer->getName());
        listItem->setSubText(customer->getNote());

        this->slist->addItem(listItem);
    }
}
